/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License")
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "secure_element_reader.h"
#include "secure_element_common.h"
#include "secure_element_session.h"
#include "native/ffi_remote_data.h"
#include "session.h"

namespace OHOS {
namespace SE {
namespace OMAPI {
void SEReaderImpl::SetReader(std::shared_ptr<Reader> rd)
{
    reader_ = rd;
}

std::string SEReaderImpl::GetName(int32_t *errorCode)
{
    std::string name = "";
    if (reader_ == nullptr) {
        ErrorLog("SEReaderImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return name;
    }
    int32_t statusCode = reader_->GetName(name);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEReaderImpl: GetName failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return "";
    }
    return name;
}

bool SEReaderImpl::IsSecureElementPresent(int32_t *errorCode)
{
    bool res = false;
    if (reader_ == nullptr) {
        ErrorLog("SEReaderImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return res;
    }
    int32_t statusCode = reader_->IsSecureElementPresent(res);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEReaderImpl: IsSecureElementPresent failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return false;
    }
    return res;
}

int64_t SEReaderImpl::OpenSession(int32_t *errorCode)
{
    if (reader_ == nullptr) {
        ErrorLog("SEReaderImpl instance is nullptr");
        *errorCode = CJ_UNSUPPORTED_ERR;
        return CJ_INVALID_VALUE_64;
    }
    std::shared_ptr<Session> session;
    int32_t statusCode = reader_->OpenSession(session);
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEReaderImpl: OpenSession failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
        return CJ_INVALID_VALUE_64;
    }
    auto se = FFIData::Create<SESessionImpl>();
    if (se == nullptr) {
        ErrorLog("SESessionImpl instance is nullptr");
        *errorCode = CJ_ILLEGAL_STATE_ERR;
        return CJ_INVALID_VALUE_64;
    }
    se->SetReader(this);
    se->SetSession(session);
    return se->GetID();
}

void SEReaderImpl::CloseSessions(int32_t *errorCode)
{
    if (reader_ == nullptr) {
        ErrorLog("SEReaderImpl instance is nullptr");
        *errorCode = CJ_ILLEGAL_STATE_ERR;
        return ;
    }
    int32_t statusCode = reader_->CloseSessions();
    if (statusCode != ErrorCode::SE_SUCCESS) {
        ErrorLog("SEReaderImpl: CloseSessions failed");
        *errorCode = Convert2CJErrorCode(static_cast<ErrorCode>(statusCode));
    }
}
}  // namespace OMAPI
}  // namespace SE
}  // namespace OHOS